import { _decorator, AudioSource, Collider, Component, EventTouch, ITriggerEvent, Node, systemEvent, SystemEvent, Touch } from 'cc';
import { Constant } from '../framework/Constant';
const { ccclass, property } = _decorator;

@ccclass('SelfPlane')
export class SelfPlane extends Component {

    public lifeValue = 5;
    public isDie = false;
    private _currLife = 0;
    private _audioSource: AudioSource = null;

    onEnable() {
        const collider = this.getComponent(Collider);
        collider.on('onTriggerEnter', this._onTriggerEnter, this);
    }

    onDisable() {
        const collider = this.getComponent(Collider);
        collider.off('onTriggerEnter', this._onTriggerEnter, this);
    }

    public start() {
        this._audioSource = this.getComponent(AudioSource);
    }

    public init() {
        this._currLife = this.lifeValue;
        this.isDie = false;
    }

    private _onTriggerEnter(event: ITriggerEvent) {
        const collisionGroup = event.otherCollider.getGroup();
        // 若是敌方飞机或子弹
        if (collisionGroup === Constant.CollisionType.ENEMY_PLANE || collisionGroup === Constant.CollisionType.ENEMY_BULLET) {
            this._currLife--;
            if (this._currLife <= 0) {
                this.isDie = true;
                this._audioSource.play();
            }
        }
    }
}


